package com.jwb.pub.controller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.model.Page;
import com.jwb.pub.service.DeptService;
import com.jwb.pub.service.UserDictService;
import com.jwb.pub.service.UserInfoService;
import com.jwb.util.MainController;
import com.jwb.util.Md5Str;
import com.jwb.util.ToolsUtil;
@Slf4j
@Controller
@RequestMapping(value="/pub/userdict" )
public class UserDictController extends MainController{


	@Autowired
	private BaseDAO baseDAO;
	@Autowired
	private UserDictService userDictService;
	
	/**
     * 返回字典信息及字典数据项 
     * 字典信息带值
     * http://127.0.0.1:8080/ssm/pub/userdict/userDictDetailList?jsonStr={"dict_id":"1"}        *  
     * @param req
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/userDictDetailList")    
    @ResponseBody
    public String  userDictDetailList(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();       	
    	String jsonStr = req.getParameter("jsonStr") == null ? "" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);			
			jsobject = userDictService.userDictDetailList(jsdata);
			
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("userDictDetailList():" + jsobject.toString());
        return jsobject.toString();  	
    	
    } 
	
    
    
    /**
     * 字典保存 
     * 字典信息带值
     * http://127.0.0.1:8080/ssm/pub/userdict/userDictDetailSave?jsonStr={"dict_id":""}   
     * @param req
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/userDictDetailSave")    
    @ResponseBody
    public String  userDictDetailSave(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();       	
    	String jsonStr = req.getParameter("jsonStr") == null ? "" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();    	
    	try {
			jsdata=JSONObject.parseObject(jsonStr);		
//			JSONObject jso = new JSONObject();    
//			JSONArray jsoarr = new JSONArray(); 
//			jsdata.put("dict_id", "2257");
//			jsdata.put("code", "testdict");
//			jsdata.put("code_desc", "字典测试");
//			jsdata.put("remarks", "字典测试");
//			jsdata.put("dict_logo", "字典测试");
//			jsdata.put("display_order", "1");			
//			jso = new JSONObject();  
//			jso.put("code", "1");
//			jso.put("code_desc", "字典1");
//			jso.put("remarks", "字典测试");
//			jso.put("dict_logo", "字典测试");
//			jso.put("display_order", "1");
//			jsoarr.add(jso);
//			jso = new JSONObject();  
//			jso.put("code", "2");
//			jso.put("code_desc", "字典2");
//			jso.put("remarks", "字典测试");
//			jso.put("dict_logo", "字典测试");
//			jso.put("display_order", "2");
//			jsoarr.add(jso);
//			jso = new JSONObject();  
//			jso.put("code", "3");
//			jso.put("code_desc", "字典3");
//			jso.put("remarks", "字典测试");
//			jso.put("dict_logo", "字典测试");
//			jso.put("display_order", "3");
//			jsoarr.add(jso);
//			jsdata.put("detaillist", jsoarr);	
			jsobject = userDictService.userDictDetailSave(jsdata);
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("userDictDetailSave():" + jsobject.toString());
        return jsobject.toString();  	
    	
    } 
	
	
    /**
     * 返回字典信息及字典数据项 
     * 字典信息带值
     * http://127.0.0.1:8080/ssm/pub/userdict/udDetailList?jsonStr={%22keyword%22:%221%22}
     * http://127.0.0.1:8080/ssm/pub/userdict/udDetailList?jsonStr={"keyword":""}        *  
     * @param req
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/udDetailList")    
    @ResponseBody
    public String  udDetailList(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();       	
    	String currentPageNbr= req.getParameter("currentPageNbr") == null ? "1" : req.getParameter("currentPageNbr");
    	String pageSize= req.getParameter("pageSize") == null ? "10" : req.getParameter("pageSize");    
    	String jsonStr = req.getParameter("jsonStr") == null ? "" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);		
			this.pageInit();
			this.getPage().setEveryPageCnt(Integer.parseInt(pageSize));
			this.getPage().setCurrentPageNbr(Integer.parseInt(currentPageNbr));
			jsobject = userDictService.udDetailList(jsdata,page); 
			
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("udDetailList():" + jsobject.toString());
        return jsobject.toString();  	
    	
    } 
    
    
    /**     
     * 字典删除 
     * http://127.0.0.1:8080/ssm/pub/userdict/udDel?jsonStr={"dict_id":"2026"}        *  
     * @param req
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/udDel")    
    @ResponseBody
    public String  udDel(HttpServletRequest req,HttpServletResponse response) throws Exception {
    
    	JSONObject jsobject = new JSONObject();       	
    	String jsonStr = req.getParameter("jsonStr") == null ? "" : req.getParameter("jsonStr");
    	JSONObject jsdata = new JSONObject();
    	try {

			jsdata=JSONObject.parseObject(jsonStr);				
			jsobject = userDictService.udDel(jsdata); 
			
    	
		} catch (Exception e) {
			e.printStackTrace();
			jsobject.put("respCode", "-3");
			jsobject.put("respDesc", "字符串格式有误");	

		}
    	log.info("udDel():" + jsobject.toString());
        return jsobject.toString();  	
    	
    } 
	
}
